/*
 * @Author: LIFM0623 1944099171@qq.com
 * @Date: 2024-02-24 16:13:45
 * @LastEditors: LIFM0623 1944099171@qq.com
 * @LastEditTime: 2024-03-05 14:02:02
 * @FilePath: \scene_back_nest\src\group\group.controller.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { Body, Controller, Delete, Get, Param, Patch, Post, Req, UseGuards } from '@nestjs/common';
import { GroupService } from './group.service';
import { RawGroupDto } from './dto/create-group.dto';
import { Request } from 'express';
import { UpdateGroupDto } from './dto/update-group.dto';
import { Public } from 'src/common/decorator/public.decorator';
import { RolesGuard } from 'src/common/guards/role.guard';
import { Roles } from 'src/common/decorator/role.decorator';
import { userRoleEnum } from 'src/enums/userRoleEnum';
import { UpdateUserToGroupDto } from 'src/group/dto/update-usertogroup.dto';
import { RawUserToGroupDto } from 'src/group/dto/create-usertogroup.dto';

@Controller('group')
export class GroupController {
  constructor(
    private readonly groupService: GroupService
  ) { }
  @Post('createGroup')
  createGroup(@Body() rawGroupDto: RawGroupDto, @Req() request: Request) {
    return this.groupService.createGroup(rawGroupDto, request.user);
  }

  @Get('getInviteCode/:groupuuid')
  getInviteCode(@Param('groupuuid') groupuuid: string) {
    return this.groupService.getInviteCode(groupuuid)
  }

  @Get('refreshInviteCode/:groupuuid')
  refreshInviteCode(@Param('groupuuid') groupuuid: string) {
    return this.groupService.refreshInviteCode(groupuuid)
  }

  @Get('isInviteCodeExpired/:invitecode')
  isInviteCodeExpired(@Param('invitecode') invitecode: string) {
    return this.groupService.isInviteCodeExpired(invitecode)
  }

  @Get('getGroupInfoById/:id')
  getGroupInfoById(@Param('id') id: string) {
    return this.groupService.getGroupInfoById(id)
  }

  @Patch('updateGroupInfo/:id')
  updateGroupInfo(@Param('id') id: string, @Body() updateGroupDto: UpdateGroupDto,@Req() request: Request) {
    return this.groupService.updateGroupInfo(id, updateGroupDto,(request.user as any).id);
  }

  @Delete('deleteGroup/:id')
  deleteGroup(@Param('id') id: string) {
    return this.groupService.deleteGroup(id)
  }


  // set 用户和小组之间的关系
  // 获取当前该用户创建小组的数量  应该<=3
  @Get('getUserIsCreatorCount')
  getUserIsCreatorCount(@Req() request: Request){
    return this.groupService.getUserIsCreatorCount((request.user as any).id)
  }

  @Post('createUserToGroupItem')
  createUserToGroupItem(@Body() rawUserToGroupDto:RawUserToGroupDto,@Req() request: Request){
    const user = request.user;
    rawUserToGroupDto.useruuid = (user as any).id;
    return this.groupService.createUserToGroupItem(rawUserToGroupDto)
  }

  @Get('joinGroup/:invitecode')
  joinGroup(@Param('invitecode') invitecode: string,@Req() request: Request){
    return this.groupService.joinGroup(request.user,invitecode)
  }

  @Get('getUserToGroupInfo')
  getUserToGroupInfo(@Req() request: Request){
    const useruuid = (request.user as any).id
    return this.groupService.getUserToGroupInfo(useruuid);
  }

  @Get('getGroupToUserInfo/:groupuuid')
  getGroupToUserInfo(@Param('groupuuid') groupuuid: string){
    return this.groupService.getGroupToUserInfo(groupuuid)
  }

  @Patch('updateUserToGroupItem/:id')
  updateUserToGroupItem(@Param('id') id: string,@Body() updateUserToGroupDto:UpdateUserToGroupDto){
    return this.groupService.updateUserToGroupItem(id, updateUserToGroupDto);
  }

  @Delete('deleteUserToGroupItem/:id')
  deleteUserToGroupItem(@Param('id') id: string){
    return this.groupService.remove(id);
  }
}
